<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php include('menu.php'); ?>

<script>
function open_popup(debid){
if(confirm('ยืนยันการพิมพ์ใบแจ้งหนี้')==true)
	{
	var link = 'bill.php?id=' + debid;
	window.open(link ,null,"height=550,width=430,status=no,toolbar=no,menubar=no,location=no,scrollbars=1");
	}
}
</script>
</head>
<body>
<?php
include("../connect/connect.php");

//ค้นหาสินค้า
if($_POST["code_search"] != "" OR $_POST["name_search"] != "" OR $_POST["category_search"] != "" OR $_POST["quantity_search"] != "" ) {
	
		$Query = "SELECT * FROM debtor WHERE deb_status = '0' ";
	
	if($_POST["code_search"] != "") {
		$Query .= "AND deb_no LIKE '%".$_POST["code_search"]."%' ";
	}
	if($_POST["name_search"] != "") {
		$Query .= "AND deb_date LIKE '%".$_POST["name_search"]."%' ";
	}
	if($_POST["category_search"] != "") {
		$Query .= "AND deb_name LIKE '%".$_POST["category_search"]."%' ";
	}
	if($_POST["quantity_search"] != "") {
		$Query .= "AND deb_amount LIKE '%".$_POST["quantity_search"]."%' ";
	}
		$Query .= " ORDER BY deb_no DESC ";
	
} else {
	$Query = "SELECT * FROM debtor WHERE deb_status = '0' ORDER BY deb_no DESC";
}
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");


//ยืนยันชำระเงิน
if(isset($_GET["id"])){
	$sql = "UPDATE debtor SET deb_status = '1' WHERE deb_id = '".$_GET["id"]."'";
	$query = mysql_query($sql);
	
	$select = "SELECT deb_amount,deb_cost FROM debtor WHERE deb_id = '".$_GET["id"]."'";
	$squery = mysql_query($select) or die ("Error Query [".$select."]");
	$row = mysql_fetch_assoc($objQuery);
	$amount = $row["deb_amount"];
	$cost = $row["deb_cost"];
	
	$strSQL = "INSERT INTO sale (date,amount,cost) VALUES ('".date("Y-m-d")."','".$amount."','".$cost."') ON DUPLICATE KEY UPDATE amount=amount+'".$amount."' , cost=cost+'".$cost."'";
	$dbquery = mysql_query($strSQL);
	
	echo "<script>window.location='$_SERVER[PHP_SELF]'</script>";
}
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>จัดการลูกหนี้</h1>
    </div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
            <td width="16%" class="center">รหัสใบแจ้งหนี้</td>
            <td width="19%" class="center">วันที่</td>
            <td width="32%" class="center">ชื่อลูกค้า</td>
            <td width="13%" class="center">ยอดเงิน</td>
            <td width="20%" class="center">สถานะ</td>
          </tr>
        </thead>
        <tbody>
		<form action="<?php echo $_SERVER["PHP_SELF"];?>" name="formsearch" method="post" id="formsearch">
          <tr class="filter">
            <td align="center"><input name="code_search" type="text" id="code_search" style="text-align: center;" /></td>
            <td align="center"><input name="name_search" type="text" id="name_search" style="text-align: center;" /></td>
            <td align="center"><input name="category_search" type="text" id="category_search" style="text-align: center;" /></td>
            <td align="center"><input name="quantity_search" type="text" id="quantity_search" style="text-align: center;" /></td>
            <td align="center"><a onclick="formsearch.submit();" class="button"><span>ค้นหา</span></a></td>
          </tr>
		  </form>
<?php
	while($row = mysql_fetch_array($objQuery))
		{
?>  
          <tr>
            <td style="text-align: center;"><?php echo $row["deb_no"];?></td>
            <td class="center"><?php echo $row["deb_date"];?></td>
            <td class="left" style="padding-left:30px"><?php echo $row["deb_name"];?></td>
            <td class="right" style="padding-right:30px"><?php echo $row["deb_amount"];?></td>
            <td class="center">[ <a href="#" onclick="open_popup(<?php echo $row["deb_id"];?>);">พิมพ์ใบเสร็จ</a> ] [ <a href="javascript:if(confirm('ยืนยันการชำระเงิน')==true){window.location='<?php echo $_SERVER['PHP_SELF'];?>?id=<?php echo $row["deb_id"];?>';}">รับชำระเงิน</a> ]</td>
          </tr>
<?php
		}
?>
        </tbody>
      </table>
  </div>
</div>

</body>
</html>
